package cn.com.surker.sale.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.enums.sale.ContractCategoryEnum;
import cn.com.surker.sale.dao.*;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.sale.entity.SaleContractCredit;
import cn.com.surker.sale.service.SaleContractCreditBaseService;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* <p>
    * 销售授信记录表 服务实现类
    * </p>
*
* @author admin
* @since 2024-07-18
*/
public abstract class SaleContractCreditBaseServiceImpl extends ServiceImpl<SaleContractCreditDao, SaleContractCredit> implements SaleContractCreditBaseService {

	@Resource
	protected SaleContractCreditDao mapper;
	@Resource
	protected SaleContractDao contractDao;
	@Resource
	protected SaleContractPumpPriceDao pumpPriceDao;
	@Resource
	protected SaleContractStrengthPriceDao strengthPriceDao;
	@Resource
	protected SaleContractSubsidyDao contractSubsidyDao;
	@Resource
	protected SaleContractTicketDao contractTicketDao;
	@Resource
	protected SaleManRecordDao manRecordDao;
	@Resource
	protected SaleStrengthAdjustPriceDao adjustPriceDao;
	@Resource
	protected SaleStrengthSpecialPriceDao specialPriceDao;


	/**
	 * 销售授信记录表 分页
	 *
	 * @param search     关键字检索
	 * @param status     状态
	 * @param orgId      公司ID
	 * @param sourceType 信控来源类型(0:回款授信，1：退款授信，2：合同评审，3：临供授信)
	 * @param credit     授信类型(0:金额1:天数)'
	 * @param offset
	 * @param limit
	 * @return
	 */
	@Override
	public IPage<SaleContractCredit> queryPage(String search, Integer status, Integer orgId, Integer sourceType, Integer credit, Integer offset, Integer limit) {
		PageHelper.offsetPage(offset, limit);
		Page<SaleContractCredit> page = new Page<>((offset + limit) / limit, limit);
		IPage<SaleContractCredit> iPage = mapper.queryPage(page, search, status, orgId, sourceType, credit);
		if (ObjectUtil.isNotEmpty(iPage) && CollUtil.isNotEmpty(iPage.getRecords())) {
			List<SaleContractCredit> records = iPage.getRecords();
			for (SaleContractCredit salecredit : records) {
				if (StrUtil.isNotEmpty(salecredit.getContractCategory())) {
					String descByCode = ContractCategoryEnum.getDescByCode(Integer.valueOf(salecredit.getContractCategory()));
					salecredit.setContractCategory(descByCode);
				}
			}
			iPage.setRecords(records);
		}
		if (CollUtil.isNotEmpty(iPage.getRecords())) {
			PageInfo<SaleContractCredit> pageInfo = new PageInfo<>(iPage.getRecords());
			if (ObjectUtil.isNotEmpty(pageInfo) && ObjectUtil.isNotEmpty(pageInfo.getTotal())) {
				iPage.setTotal(pageInfo.getTotal());
			}
		}
		return iPage;
	}

	@Override
	public RestResultDto updateStatus(Long id, Integer status) {
		SaleContractCredit dbCredit = getById(id);
		if (ObjectUtil.isEmpty(dbCredit)) {
			RestResultDto.fail("当前销售记录表不存在!");
		}
		//TODO 作废的逻辑处理------------------------------------------未完成
		dbCredit.setStatus(status);
		updateById(dbCredit);

		return RestResultDto.success("保存成功");
	}


	/**
	 * 作废授信内容
	 * @param contractId
	 * @param credit
	 * @return
	 */
	public Integer deleteCredit(Long contractId, Integer credit, String creator) {
		if (StringUtils.isAllParamNull(contractId, credit))
			return Code.PARAM_ERROR.getCode();
		Date nowDate =new Date();
		String time = DateUtil.convertString(DateUtil.convertBeforeDayDate(nowDate, 5));
		QueryWrapper<SaleContractCredit> query = new QueryWrapper<>();
		query.eq("contract_id", contractId);
		query.eq("credit", credit);
		query.eq("status", 0);
		query.ge("create_time", time);
		query.orderByDesc("id");
		List<SaleContractCredit> list = mapper.selectList(query);
		if (list == null || list.size() == 0)
			return Code.ERROR_DATA.getCode();
		SaleContractCredit contractCredit = list.get(0);
        contractCredit.setUpdateTime(nowDate);
        contractCredit.setUpdator(creator);
        Integer code = mapper.updateById(contractCredit);
        if (code > 0)
            return 0;
		return -1;
	}
	/**
	 * 复制销售合同信息
	 * @param oldOrgId
	 * @param newOrgId
	 * @return
	 */
	public RestResultDto copyContractAll(Long oldOrgId, Long newOrgId) {

		return null;
	}

}